package file

import (
	"encoding/json"
	"net/http"

	"chainmaker.org/chainmaker/smarteditor/common"
	"chainmaker.org/chainmaker/smarteditor/conf"
	"chainmaker.org/chainmaker/smarteditor/session"
	"github.com/88250/gulu"
)

type RetHiddenList struct {
	ContractList []conf.UserContractHidden `json:"contractList"`
	Total        int                       `json:"total"`
}

func HiddenContractListHandler(w http.ResponseWriter, r *http.Request) {
	uid, err := session.GetUidFromSession(r)
	if err != nil {
		log.Warnf("HiddenContractListHandler Forbidden. url:%s, uid:%s, err:%v", r.URL, uid, err)
		http.Error(w, "Forbidden", http.StatusForbidden)
		return
	}

	result := gulu.Ret.NewResult()
	defer gulu.Ret.RetResult(w, r, result)
	// 解析page 和 last_ts
	var args map[string]interface{}
	if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
		log.Warnf("HiddenContractListHandler decode parameter failed. url:%s, err:%v", r.URL, err)
		result.Code = -1
		result.Msg = "invalid param"
		return
	}
	pageO, ok := args["page"]
	page := int64(1)
	if ok {
		page, _ = pageO.(int64)
		if page <= 0 {
			page = 1
		}
	}
	pageSizeO, ok := args["pageSize"]
	pageSize := int64(-1)
	if ok {
		pageSize, _ = pageSizeO.(int64)
		if pageSize <= 0 {
			pageSize = -1
		}
	}
	hiddenList, total, queryErr := conf.GetUserContractHiddenList(uid, page, pageSize)
	if queryErr != nil {
		result.Code = -2 // 服务内部错误
		result.Msg = common.ErrorInternal
		return
	}
	var retList RetHiddenList
	retList.Total = total
	retList.ContractList = hiddenList
	result.Data = retList
}

func SetContractHiddenStatus(w http.ResponseWriter, r *http.Request) {
	uid, err := session.GetUidFromSession(r)
	if err != nil {
		log.Warnf("SetContractHiddenStatus Forbidden. url:%s, uid:%s, err:%v", r.URL, uid, err)
		http.Error(w, "Forbidden", http.StatusForbidden)
		return
	}

	result := gulu.Ret.NewResult()
	defer gulu.Ret.RetResult(w, r, result)
	// 解析page 和 last_ts
	var args map[string]interface{}
	if err := json.NewDecoder(r.Body).Decode(&args); err != nil {
		log.Warnf("SetContractHiddenStatus decode parameter failed. url:%s, err:%v", r.URL, err)
		result.Code = -1
		result.Msg = "invalid param"
		return
	}

	contractNameO, ok := args["contractName"]
	if !ok {
		http.Error(w, "parameter error", http.StatusBadRequest)
		return
	}
	contractName, cOk := contractNameO.(string)
	if !cOk || contractName == "" {
		http.Error(w, "parameter error", http.StatusBadRequest)
		return
	}
	hiddenO, ok := args["hidden"]
	if !ok {
		http.Error(w, "parameter error", http.StatusBadRequest)
		return
	}
	hidden, hOk := hiddenO.(bool)
	if !hOk {
		http.Error(w, "parameter error", http.StatusBadRequest)
		return
	}
	hiddenV := int8(0)
	if hidden {
		hiddenV = 1
	}
	updateErr := conf.UpdateUserContractHidden(uid,
		contractName, hiddenV)
	if updateErr != nil {
		log.Errorf("SetContractHiddenStatus uid %s, contract %s,failed %s",
			uid, contractName, updateErr.Error())
		result.Code = -1
		result.Msg = updateErr.Error()
		return
	}
}
